import Axios from '@/modules/Axios'
import DownloadFile from '@/modules/DownloadFile'

export default class ResourceModel {

    constructor(endpoint) {
        this.endpoint = endpoint;
    }


    list(params,mode) {
        return new Promise((resolve,reject)=>{
            if(mode == undefined){
                mode = 0;
            }else{
                mode = mode ? 0 : 1;
            }
            Axios.get(this.endpoint,{...params,mode:mode})
            .then((response) => {
                resolve(response.data);
            });
        });
    }
    show(id) {
        return new Promise((resolve,reject) => {
            Axios.get(this.endpoint+'/'+id)
            .then((response) => {
                resolve(response.data);
            });
        });
    }
    store(data){
        return new Promise((resolve,reject) => {
            Axios.post(this.endpoint,data)
            .then((response) => {
                resolve(response.data);
            });
        });
    }

    put(id,data){
        return new Promise((resolve,reject) => {
            Axios.put(this.endpoint+'/'+id,data)
            .then((response) => {
                resolve(response.data);
            });
        });
    }

    delete(id){
        return new Promise((resolve,reject) => {
            Axios.delete(this.endpoint+'/'+id)
            .then((response) => {
                resolve(response.data);
            });
        });
    }
    download(params) {
        DownloadFile('/api'+this.endpoint,params);
    }
}
